package com.itboye.system.helper;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * ByUniqueIdHelper.java
 * Created on  2016/8/1 08:35
 * 版本       修改时间          作者      修改内容
 * V1.0.1    2016/8/1      hebidu    初始版本
 * <p>
 * Copyright (c) 2013 杭州博也网络科技有限公司 版权所有
 * HANGZHOU BOYE TECHNOLOGY CO.,LTD. All Rights Reserved.
 */
public class ByLocalHostHelper {

    public static String getLocalMac() throws SocketException, UnknownHostException {
        InetAddress inetAddress = InetAddress.getLocalHost();
        byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();

        //下面代码是把mac地址拼装成String
        StringBuffer sb = new StringBuffer();

        for(int i=0;i < mac.length;i++){
            if(i!=0){
                sb.append("-");
            }
            //mac[i] & 0xFF 是为了把byte转化为正整数
            String s = Integer.toHexString(mac[i] & 0xFF);
            sb.append(s.length()==1?0+s:s);
        }

        //把字符串所有小写字母改为大写成为正规的mac地址并返回
        return sb.toString().toUpperCase();
    }
}
